"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : SQL语法
Case Name   : 收回用户在当前schema的所有权限,执行create table as
Create At   : 2023/03/31
Owner       : @zou_jialiang050
Description :
    1.创建用户
    2.创建源表
    3.收回用户在当前模式的全部权限
    4.以新建用户身份进入当前模式,执行create table as
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.合理报错
    5.成功
History     :
"""

import os
import unittest
from yat.test import macro
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class SQL(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.com = Common()
        self.Constant = Constant()
        self.table1 = 't_create_table_as_045_01'
        self.table2 = 't_create_table_as_045_02'
        self.user = 'u_create_table_as_045'

    def test_create_table_as(self):
        text = 'text1:创建用户 expect:成功'
        self.logger.info(text)
        create_user = self.primary_sh.execut_db_sql(
            f'''drop user if exists {self.user};
            create user {self.user} with password '{macro.COMMON_PASSWD}';''')
        self.logger.info(create_user)
        self.assertIn(self.Constant.DROP_ROLE_SUCCESS_MSG, create_user,
                      "执行失败" + text)
        self.assertIn(self.Constant.CREATE_ROLE_SUCCESS_MSG, create_user,
                      "建分区表失败" + text)

        text = 'text2:创建源表 expect:成功'
        self.logger.info(text)
        create_table = self.primary_sh.execut_db_sql(f'''
            drop table if exists {self.table1};
            create table {self.table1}(id int);''')
        self.logger.info(create_table)
        self.assertIn(self.Constant.TABLE_DROP_SUCCESS, create_table,
                      "执行失败" + text)
        self.assertIn(self.Constant.CREATE_TABLE_SUCCESS, create_table,
                      "建分区表失败" + text)

        text = 'text3:收回用户在当前模式的全部权限 expect:成功'
        self.logger.info(text)
        revoke_res = self.primary_sh.execut_db_sql(
            f'''revoke all privileges on schema public from {self.user};''')
        self.logger.info(revoke_res)
        self.assertIn(self.Constant.REVOKE_SUCCESS_MSG, revoke_res,
                      "执行失败" + text)

        text = 'text4:以新建用户身份进入当前模式,执行create table as ' \
               'expect:合理报错'
        self.logger.info(text)
        create_table = self.primary_sh.execut_db_sql(
            f'''set current_schema = public;drop table if exists {self.table2};
            create table {self.table2} as select * from {self.table1};''',
            sql_type=f" -U {self.user} -W {macro.COMMON_PASSWD}")
        self.logger.info(create_table)
        self.assertIn(self.Constant.SET_SUCCESS_MSG, create_table, "执行失败"
                      + text)
        self.assertIn(self.Constant.TABLE_DROP_SUCCESS, create_table,
                      "执行失败" + text)
        self.assertIn('ERROR:  permission denied for schema public',
                      create_table, "执行失败" + text)

    def tearDown(self):
        text = 'text5:清理环境 expect:成功'
        self.logger.info(text)
        clean_environment = self.primary_sh.execut_db_sql(
            f'''drop table {self.table1};drop user if exists {self.user} 
            cascade;''')
        self.logger.info(clean_environment)
        self.assertIn(self.Constant.TABLE_DROP_SUCCESS, clean_environment,
                      "执行失败" + text)
        self.assertIn(self.Constant.DROP_ROLE_SUCCESS_MSG, clean_environment,
                      "执行失败" + text)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
